

## TABLE 1

# load, combine data
library(rio)
library(tidyverse)

# generate quick tables
library(knitr)



# LOAD DATA
funds <- import("list_laws_funds.csv")
rules <- import("list_laws_rules.csv")

all_docs <- funds %>%
  full_join(rules)

number_docs <- all_docs %>%
  group_by(country) %>%
  summarise(`Number of documents` = length(country)) %>%
  rbind(c("Guatemala","0")) %>%
  arrange(country) %>%
  rename_with(str_to_title)

types_fund <- all_docs %>%
  filter(!is.na(stabilization)) %>%
  select(country,stabilization:pension) %>%
  rename_with(str_to_title) %>%
  group_by(Country) %>%
  summarise_if(is.numeric, sum) %>%
  ungroup() %>%
  pivot_longer(cols = Stabilization:Pension) %>%
  filter(value>0) %>%
  select(-value) %>%
  pivot_wider(id_cols = "Country", values_from = "name") %>%
  unite("Type of fund", Investment:Savings, sep = ", ", na.rm = T)

types_rule <- all_docs %>%
  filter(!is.na(related_rule)) %>%
  select(country,related_rule) %>%
  rename_with(str_to_title) %>%
  unique() %>%
  separate(col = "Related_rule", sep = ", ", into = c("Rule1", "Rule2", "Rule3", "Rule4")) %>%
  pivot_longer(cols = Rule1:Rule4) %>%
  mutate_all(str_to_title) %>%
  select(Country,value) %>%
  filter(!is.na(value)) %>%
  unique() %>%
  pivot_wider(id_cols = "Country", names_from = "value") %>%
  unite("Type of rule", Revenue:Expenditure, sep = ", ", na.rm = T) %>%
  mutate(Country = ifelse(Country == "Trinidad And Tobago", "Trinidad and Tobago", Country))

table1 <- number_docs %>%
  left_join(types_fund) %>%
  left_join(types_rule) %>%
  mutate_at(vars(`Type of fund`:`Type of rule`), ~replace_na(., "-")) %>%
  unique()

kable(table1, "latex", booktabs = TRUE)


# documents that mention both funds and rules
all_docs %>%
  filter(!is.na(stabilization) & !is.na(related_rule))
